/*
 * @Author: wangc
 * @Date: 2020-07-28 19:26:51
 * @LastEditTime: 2020-08-29 21:27:43
 * @Description: 
 */
import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/pc/Login.vue'

Vue.use(VueRouter)

// 解决Vue-Router升级导致的Uncaught(in promise) navigation guard问题
const originalPush = VueRouter.prototype.replace
VueRouter.prototype.replace = function replace(location, onResolve, onReject) {
    if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
    return originalPush.call(this, location).catch(err => err)
}
const routes = [{
        path: '/',
        name: 'Login',
        component: () =>
            import ('../views/pc/Remuse.vue'),
        meta: {
            title: "王超的简历"
        }
    },
    {
        path: '/login',
        name: 'Login',
        component: Login,
        meta: {
            title: "登录"
        }
    },
    {
        path: '/register',
        name: 'Register',
        component: () =>
            import ('../views/pc/Register.vue'),
        meta: {
            title: "注册"
        }
    },
    {
        path: '/main',
        name: 'Main',
        redirect: '/main/message',
        component: () =>
            import ('../views/pc/Main.vue'),
        meta: {
            title: "后台首页"
        },
        children: [{
            path: 'message', //以“/”开头的嵌套路径会被当作根路径，所以子路由上不用加“/”;在生成路由时，主路由上的path会被自动添加到子路由之前，所以子路由上的path不用在重新声明主路由上的path了。
            name: 'message',
            component: () =>
                import ('../views/pc/Message.vue'),
        }, {
            path: 'user', //以“/”开头的嵌套路径会被当作根路径，所以子路由上不用加“/”;在生成路由时，主路由上的path会被自动添加到子路由之前，所以子路由上的path不用在重新声明主路由上的path了。
            name: 'user',
            component: () =>
                import ('../views/pc/User.vue'),
        }]
    }
]

const router = new VueRouter({
    mode: 'history',
    routes
})

router.beforeEach((to, from, next) => {
    if (to.path === '/login' || to.path === '/register' || to.path === '/') {
        next();
    } else {
        let token = localStorage.getItem("token");
        if (!token) {
            alert("登录失效")
            next('/login');
        } else {
            next();
        }
    }
})
export default router